/**
 * 公共弹窗管理
 * Name = uiManager
 * DateTime = Sat Jan 15 2022 23:40:11 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = uiManager.ts
 * gitee： https://gitee.com/coyeking
 */
 
import { _decorator, Component, Node, resources, Prefab, instantiate,find,director} from 'cc';
import { ModeluType } from '../define/ccenum';
import { poolManager } from './poolManager';
 
export class uiManager {
	/**
     * 显示Alert
     * @param msg 
     * @param param 
     * @param type 
     */
     public static showAlert(msg:string,param?: any,type:ModeluType=ModeluType.DEFAULT):void{
        resources.load("prefab/box/alert",Prefab, function(err, prefab:Prefab) {
            if (err) {console.log('载入预制资源失败， 原因:' + err); return};
            let tipPrefab:Node = instantiate(prefab)
            let tipScript = tipPrefab.getComponent("alert")
            //@ts-ignore
            tipScript.init(msg,param,type);
            find('Canvas').addChild(tipPrefab);
        });
    };
    
    /**
     * 显示Toast
     * @param {string} msg
     * @param {ModeluType} 类型
     */
     public static showToast(msg:string,type:ModeluType=ModeluType.DEFAULT):void{
        resources.load("prefab/box/toast",Prefab, function(err, prefab:Prefab) {
            if (err) {console.log('载入预制资源失败， 原因:' + err); return}

            let tipPrefab:Node = instantiate(prefab)
            let tipScript = tipPrefab.getComponent("toast")
            //@ts-ignore
            tipScript.init(msg,type)
            find('Canvas').addChild(tipPrefab)
        });
    };
    
    /**
     * @param {bshow} boolean
     */
     public static showWaiting(bshow:boolean=false):void{
        let node = find('Canvas').getChildByName('waiting');
        if (!bshow){
            if (node) node.destroy();
            return;
        }
        if (node) return;

        resources.load("prefab/box/waiting",Prefab, function(err, prefab:Prefab) {
            if (err) {console.log('载入预制资源失败， 原因:' + err); return}
            let tipPrefab:Node = instantiate(prefab)
            find('Canvas').addChild(tipPrefab)
        });
    }

    /**
     * 新建窗口
     * @param path 
     * @param classname 
     * @param param 
     */
    public static createLayer(path:string,classname:string,param:any=null){
        
        let target = find('Canvas').getChildByName('layer') as Node;
        if (!target){console.log('加载失败，Canvas下缺少layer节点'); return};
        resources.load(path,Prefab, function(err, prefab:Prefab) {
            if (err) {console.log('载入预制资源失败， 原因:' + err); return};
            let node = poolManager.instance.getNode(prefab,target) as Node;
            if (!param) return;
            let tipScript = node.getComponent(classname);
            //@ts-ignore
            tipScript.init(param);
        });
    }
}

